﻿Imports System.Text

Public Class Form1

    Private UNSPECIFIED As String = "unspecified"

    Private FINAL_PAGE As String = "pageFinal"
    Private NAME_PAGE As String = "page1"
    Private FRUIT_PAGE As String = "page2"
    Private GENDER_PAGE As String = "page3"

    Private Sub btnClickMe_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOk.Click

        calculateMessage()
        tabPanel.SelectTab(FINAL_PAGE)

    End Sub

    Private Function getGender() As String

        Dim gender As String = UNSPECIFIED
        For Each rdo As RadioButton In genders.Controls
            If rdo.Checked Then
                gender = rdo.Text
            End If
        Next
        Return gender
    End Function

    Private Sub getFavoriteFruits(ByVal sb As StringBuilder)

        Dim atLeastOneChecked As Boolean = False
        For Each ctl As Control In tabPanel.TabPages(FRUIT_PAGE).Controls
            If TypeOf ctl Is CheckBox AndAlso CType(ctl, CheckBox).Checked Then
                sb.Append(ctl.Text).Append(" and ")
                atLeastOneChecked = True
            End If
        Next

        If atLeastOneChecked Then
            sb.Remove(sb.Length - 5, 5)
        Else
            sb.Append(UNSPECIFIED)
        End If

    End Sub



    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
        For Each ctl As Control In Me.Controls
            resetControl(ctl)
        Next
        lblOutput.Text = ""
    End Sub

    Private Sub resetControl(ByVal control As Control)

        For Each ctl As Control In control.Controls
            If TypeOf ctl Is TextBox Then
                CType(ctl, TextBox).Text = ""
            ElseIf TypeOf ctl Is CheckBox Then
                CType(ctl, CheckBox).Checked = False
            ElseIf TypeOf ctl Is RadioButton Then
                CType(ctl, RadioButton).Checked = False
            Else
                resetControl(ctl)
            End If
        Next

    End Sub


    Private Function getNameText() As String

        If String.IsNullOrEmpty(txtFirstName.Text) And String.IsNullOrEmpty(txtLastName.Text) Then
            Return "nameless"
        Else
            Return txtFirstName.Text + " " + txtLastName.Text
        End If

    End Function

    Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
        If tabPanel.SelectedIndex < tabPanel.TabCount Then
            tabPanel.SelectedIndex = tabPanel.SelectedIndex + 1
        End If

    End Sub

    Private Sub calculateMessage()
        Dim sb As New StringBuilder
        constructNameMessage(sb)
        constructGenderMessage(sb)
        constructFruitMessage(sb)
        lblOutput.Text = sb.ToString
    End Sub

    Private Sub constructNameMessage(ByVal sb As StringBuilder)
        sb.Append("Hello ").Append(getNameText).Append(vbNewLine)
    End Sub

    Private Sub constructGenderMessage(ByVal sb As StringBuilder)
        sb.Append("You are of the gender ").Append(getGender).Append(vbNewLine)
    End Sub

    Private Sub constructFruitMessage(ByVal sb As StringBuilder)
        sb.Append("and your favorite fruits are ")
        getFavoriteFruits(sb)
    End Sub


    Private Sub tabPanel_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles tabPanel.SelectedIndexChanged
        tabPanel.SelectedTab.Controls(0).Focus()
        Select Case tabPanel.SelectedTab.Name
            Case FRUIT_PAGE
                chkApples.Focus()
            Case GENDER_PAGE
                rdoMale.Focus()
            Case NAME_PAGE
                txtFirstName.Focus()
            Case FINAL_PAGE
                calculateMessage()
        End Select

    End Sub

    Private Sub updateNavButtonState() Handles tabPanel.SelectedIndexChanged

        btnNext.Visible = (Not tabPanel.SelectedTab.Name.Equals(FINAL_PAGE))
        btnPrev.Visible = (Not tabPanel.SelectedTab.Name.Equals(NAME_PAGE))

    End Sub

    Private Sub btnPrev_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrev.Click
        If tabPanel.SelectedIndex > 0 Then
            tabPanel.SelectedIndex = tabPanel.SelectedIndex - 1
        End If

    End Sub

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        txtFirstName.Focus()
        updateNavButtonState()
    End Sub
End Class
